package com.example.yin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yin.common.R;
import com.example.yin.mapper.AdminMapper;
import com.example.yin.model.domain.Admin;
import com.example.yin.model.request.AdminRequest;
import com.example.yin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
//    验证用户登录,方法实现
    @Override
    public R verityPasswd(AdminRequest adminRequest, HttpSession session) {
        // 创建一个查询条件包装器
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        // 设置查询条件，根据用户名和密码进行查询
        queryWrapper.eq("name",adminRequest.getUsername());
        queryWrapper.eq("password",adminRequest.getPassword());
        // 判断数据库中是否存在匹配的记录
        if (adminMapper.selectCount(queryWrapper) > 0) {
            // 如果存在匹配的记录，则将用户名保存到会话中
            session.setAttribute("name", adminRequest.getUsername());
            // 返回登录成功的响应
            return R.success("登录成功");
        } else {
            // 如果没有匹配的记录，则返回用户名或密码错误的响应
            return R.error("用户名或密码错误");
        }
    }
}
